package com.lingxin.gps.wap.command;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.lingxin.gps.sdk.GpsDeviceApi;
import com.lingxin.gps.sdk.GpsDeviceUtil;
import com.pisces.core.BaseCommand;
import com.pisces.tool.query.QueryObject;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class CDeviceList extends BaseCommand {

	/**
	 * 设备号
	 */
	private String deviceCode;

	private Long uid;
	private String token;

	@Override
	public void init() throws Exception {
		deviceCode = getPara("deviceCode");

		uid = context.getAttr("uid");
		token = context.getAttr("token");
	}

	@Override
	public void doCommand() throws Exception {
		QueryObject queryObject = new QueryObject();
		if(StrKit.notBlank(deviceCode)){
			queryObject.addQuery("deviceCode", deviceCode);
		}

		JSONObject resData = GpsDeviceApi.deviceList(uid, token, queryObject);
		//data.put("groupList", resData.getJSONArray("groupList"));

		List<JSONObject> resGroupList = JSON.parseArray(resData.getString("groupList"), JSONObject.class);
		List<JSONObject> groupList = new ArrayList<>();
//		for(JSONObject group : resGroupList){
//			group.put("deviceList", GpsDeviceUtil.getDeviceList(group));
//			groupList.add(group);
//		}

		JSONObject topGroup = resGroupList.get(0);
		topGroup.put("deviceList", GpsDeviceUtil.getDeviceList(topGroup, false));
		groupList.add(topGroup);
		List<JSONObject> topGroupList = JSON.parseArray(topGroup.getString("groupList"), JSONObject.class);
		for(JSONObject group : topGroupList){
			group.put("deviceList", GpsDeviceUtil.getDeviceList(group, true));
			groupList.add(group);
		}
		data.put("groupList", groupList);
	}


}
